<template>
  <div class="content">
    <div class="content__nav">
      <ul class="content__nav__list">
        <li
          v-for="item in navList"
          :key="item.name"
          @click="hangdleNavClick(item.tab)"
          :class="{
            content__nav__list__item: true,
            'content__nav__list__item--active': item.tab === navActive,
          }"
        >
          {{ item.name }}
        </li>
      </ul>
    </div>
    <div class="product">
      <template v-for="item in item" :key="item._id">
        <Commodity :item="item" :name="shopName" />
      </template>
    </div>
  </div>
</template>
<script>
import { get } from "../../utils/request";
import { useRoute } from "vue-router";
import { reactive, toRefs, ref, watchEffect } from "vue";
import Commodity from "../../components/commodity/Commodity.vue";

const navList = [
  { name: "全部", tab: "all" },
  { name: "秒杀", tab: "seckill" },
  { name: "新鲜水果", tab: "fruit" },
];
//nav事件
const useNavClickEffect = () => {
  const navActive = ref(navList[0].tab);
  const hangdleNavClick = (tab) => {
    navActive.value = tab;
  };
  return { navActive, hangdleNavClick };
};
//获取商品信息
const useShopContentEffect = (navActive, id) => {
  const data = reactive({
    item: [],
  });

  const getShopContent = async () => {
    const result = await get(`api/shop/${id}/products`, {
      tab: navActive.value,
    });
    if (result?.data?.errno === 0) {
      data.item = result.data.data;
    }
  };
  watchEffect(() => {
    getShopContent();
  });
  const { item } = toRefs(data);
  return { item };
};

export default {
  name: "shopContent",
  components: { Commodity },
  props: ["shopName"],
  setup() {
    const route = useRoute();
    const id = route.params.id;
    const { navActive, hangdleNavClick } = useNavClickEffect();
    const { item } = useShopContentEffect(navActive, id);
    return {
      item,
      navList,
      hangdleNavClick,
      navActive,
      id,
    };
  },
};
</script>

<style lang="scss" scoped>
@import "../../style/viriables.scss";
@import "../../style/mixins.scss";
.content {
  position: absolute;
  display: flex;
  top: 1.54rem;
  left: 0;
  right: 0;
  bottom: 0.49rem;
}
.content__nav {
  width: 0.76rem;
  height: 100%;
  background-color: $search-bgColor;
  overflow-y: scroll;
  &__list {
    margin: 0;
    padding: 0;
    &__item {
      height: 0.4rem;
      width: 100%;
      line-height: 0.4rem;
      text-align: center;
      color: $content-fontcolor;
      font-size: 0.14rem;
      &--active {
        background-color: $color-fff;
      }
    }
  }
}
.product {
  margin: 0 0.18rem 0 0.16rem;
  flex: 1;
  overflow-y: scroll;
}
</style>